package org.example.lesson;

import java.io.IOException;
import java.net.Socket;

public class TCPThreadServer extends TCPEchoServer {
    /**
     * 构造方法
     * @param port 服务端的端口号
     * @throws IOException
     */
    public TCPThreadServer(int port) throws IOException {
        super(port);
    }

    @Override
    public void start() throws IOException {
        System.out.println("服务器已启动，等待客户端连接.");
        //循环接收客户端的连接请求
        while(true){
            //接收客户端的连接
            Socket clientSocket=socket.accept();
            //为每个客户端创建一个线程
            Thread thread=new Thread(()->{
                //子线程处理连接
                processConnections(clientSocket);
            });
            thread.start();
        }
    }

    public static void main(String[] args) throws IOException {
        TCPThreadServer server=new TCPThreadServer(9090);
        server.start();
    }
}
